package com.guli.ucenter.controller;


import com.guli.common.vo.R;
import com.guli.ucenter.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Random;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Frank
 * @since 2019-04-22
 */
@CrossOrigin
@RestController
@RequestMapping("/admin/ucenter/member")
@Api(description = "会员管理接口")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @ApiOperation(value = "统计今日会员注册数")
    @GetMapping("/count-register/{day}")
    public R registerCount(@ApiParam(value = "统计日期", name = "day", example = "2019-01-01")
                           @PathVariable("day") String day) {
        Integer count = memberService.countRegisterByDay(day);
        return R.ok().data("countRegister", count);


    }

    @GetMapping("login")
    public void login(HttpSession session, HttpServletRequest request, HttpServletResponse response) throws IOException {
        int localPort = request.getLocalPort();
        if (session.getAttribute("userId") == null) {
            String userId = String.valueOf(new Random().nextInt(100));
            session.setAttribute("userId", userId);
            response.getWriter().append("Hello," + userId + ",this is " + localPort + " port");
        } else {
            String userId = (String) session.getAttribute("userId");
            response.getWriter().append("WelcomeBack," + userId + ",this is " + localPort + " port");

        }
    }
}



